﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;  

namespace Ivory.Libs
{
    

    public class TextureBuilder
    {
        //Statics
        public static Texture2D CreateTexture(GraphicsDevice Device, int width, int height, Color color, byte alpha=255)
        {
            Texture2D texture = new Texture2D(Device, width, height, false, SurfaceFormat.Color);
            Color[] colors = new Color[width * height];
            for (int i = 0; i < colors.Length; i++)
                colors[i] = new Color(color.R, color.G, color.B, alpha);

            texture.SetData(colors);
            return texture;
        }

        //Instance
        //Fields
        private RenderTarget2D _RenderTarget; 

		private int _CanvasWidth; 
		public int CanvasWidth
		{
			get { return _CanvasWidth; }
            private set { _CanvasWidth = value; }
		}

        private int _CanvasHeight;
        public int CanvasHeight
        {
            get { return _CanvasHeight; }
            private set { _CanvasHeight = value; }
        }
         
		private RenderFlags _RenderFlags; 
		public RenderFlags RenderFlags
		{
			get { return _RenderFlags; }
			set { _RenderFlags = value; }
		}

        public bool IsCreated { get { return _RenderTarget != null; } }
        //Construct
        public TextureBuilder(Game game, int width, int height)
        {
            this.CanvasWidth = width;
            this.CanvasHeight = height;
            _RenderTarget = new RenderTarget2D(game.GraphicsDevice, width, height, false, SurfaceFormat.Color, DepthFormat.None);
        }

        //Methods       
        public Texture2D GetResult()
        {
            if (IsCreated)
                return _RenderTarget;
            else
                return null;
        }        
        public void ClearRenderFlags() {
            RenderFlags = null;
        }

        public void DrawText(Vector2 Position, SpriteFont font, Color color, string Text)
        {
            if (RenderFlags == null)
                RenderFlags = RenderFlags.Default;

            using (SpriteBatch renderer = new SpriteBatch(_RenderTarget.GraphicsDevice))
            {
                _RenderTarget.GraphicsDevice.SetRenderTarget(_RenderTarget);
                renderer.Begin();

                renderer.DrawString(font, Text, Position, color, RenderFlags.RotationFlag,
                    RenderFlags.Origin, RenderFlags.ScaleFlag, RenderFlags.EffectFlag, RenderFlags.LayerDepth);

                renderer.End();
                _RenderTarget.GraphicsDevice.SetRenderTarget(null);
            };
        }

        public void DrawColor(Vector2 Position, int width, int height, Color color)
        {
            DrawColor(new Rectangle((int)Position.X, (int)Position.Y, width, height), color);
        }
        public void DrawColor(Rectangle Position, Color color, byte alpha = 255)
        {
            if (RenderFlags == null)
                RenderFlags = RenderFlags.Default;

            using (SpriteBatch renderer = new SpriteBatch(_RenderTarget.GraphicsDevice))
            {
                _RenderTarget.GraphicsDevice.SetRenderTarget(_RenderTarget);
                renderer.Begin();

                Texture2D texture = CreateTexture(_RenderTarget.GraphicsDevice, Position.Width, Position.Height, color, alpha);

                renderer.Draw(texture, Position, null, Color.White, RenderFlags.RotationFlag,
                    RenderFlags.Origin, RenderFlags.EffectFlag, RenderFlags.LayerDepth);

                renderer.End();
                _RenderTarget.GraphicsDevice.SetRenderTarget(null);
            };
        }
        
        public void DrawTexture(Vector2 Position, Texture2D texture)
        {
            DrawTexture(new Rectangle((int)Position.X, (int)Position.Y, texture.Width, texture.Height), texture);
        }
        public void DrawTexture(Rectangle Position, Texture2D texture)
        {
            if (RenderFlags == null)
                RenderFlags = RenderFlags.Default;

            using (SpriteBatch renderer = new SpriteBatch(_RenderTarget.GraphicsDevice))
            {
                _RenderTarget.GraphicsDevice.SetRenderTarget(_RenderTarget);
                renderer.Begin();

                renderer.Draw(texture, Position, null, Color.White, RenderFlags.RotationFlag,
                    RenderFlags.Origin, RenderFlags.EffectFlag, RenderFlags.LayerDepth);

                renderer.End();
                _RenderTarget.GraphicsDevice.SetRenderTarget(null);
            };
        }
    }
}
